package com.lsc.service;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.lsc.entity.BugContent;

@Service
public class BugCatchServiceImpl implements BugCatchService {

	@Value("${bug.url:http://www.cnnvd.org.cn}")
	String url;

	@Value("${bug.keywords}")
	String keywords;

	@Value("${bug.file}")
	String checkFilePath;

	/**
	 * 解析bug列表
	 * 
	 * @param page
	 * @param buglist
	 */
	public void bugCatch(int page, String checkTime, List<BugContent> buglist) {
		try {
            boolean nextFlag=true;
			Document doc = Jsoup.connect(url + "/web/vulnerability/querylist.tag?pageno=" + page + "&repairLd=").post();
			Elements list = doc.getElementsByClass("list_list");
			Elements li = list.get(0).getElementsByTag("li");
			for (int i = 0; i < li.size(); i++) {
				BugContent bc = new BugContent();
				Elements bug = li.get(i).getElementsByClass("fl").get(0).getElementsByTag("a");
				String bugTime = li.get(i).getElementsByClass("fr").text();

				String bugTitle = bug.get(0).text();
				String href = bug.get(0).attr("href");
				String bugNum = bug.get(1).text();
//				bc.setBugTitle(bugTitle);  //过长的获取不全
				bc.setBugNum(bugNum);
				bc.setBugTime(bugTime);
                
				if(page==1 && i==0)
				{
					String[] ss=bugNum.split("-");
					String lastDate=ss[1]+ss[2];
					File checkFile = new File(checkFilePath + "/checkfile.txt");
					FileUtil.writerTxt(checkFile, lastDate);
				}
				
				String[] ss=bugNum.split("-");
				String nowTime=ss[1]+ss[2];
				
				if (checkKeyWord(bugTitle) && nowTime.compareTo(checkTime)>0) {
					getBugContent(href, bc);
					buglist.add(bc);
				}
				if(nowTime.replace("-", "").compareTo(checkTime)<=0)
				{
					nextFlag=false;
					break;
				}
			}

			if (buglist.size() > 0) {
				String lastDate = buglist.get(buglist.size() - 1).getBugNum();
				String[] ss=lastDate.split("-");
				lastDate=ss[1]+ss[2];
				if (lastDate.compareTo(checkTime) >= 0 && nextFlag) {
					bugCatch(page + 1, checkTime, buglist);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取bug详情
	 * 
	 * @param href
	 * @param bc
	 * @return
	 * @throws IOException
	 */
	private String getBugContent(String href, BugContent bc) throws IOException {
		String bugContent = "";
		Document doc = Jsoup.connect(url + href).post();
		Elements list = doc.getElementsByClass("detail_xq w770").get(0).getElementsByTag("li");
		bc.setBugLev(list.get(1).getElementsByTag("a").text());

		String bugTitle = doc.getElementsByClass("detail_xq w770").get(0).getElementsByTag("h2").text();
		bc.setBugTitle(bugTitle);

		Elements el_bug = doc.getElementsByClass("d_ldjj");
		if (el_bug.size() > 0) {
			Elements p = el_bug.get(0).getElementsByTag("p");
			String bugjs = "";
			for (int i = 0; i < p.size(); i++) {
				bugjs = bugjs + p.get(i).text()+"\r\n";
			}
			bc.setBugjs(bugjs);

			Elements p1 = el_bug.get(1).getElementsByTag("p");
			String bugg = "";
			for (int i = 0; i < p1.size(); i++) {
				bugg = bugg + p1.get(i).text()+"\r\n";
			}
			bc.setBuggg(bugg);

			Elements p2 = el_bug.get(2).getElementsByTag("p");
			String bugck = "";
			for (int i = 0; i < p2.size(); i++) {
				bugck = bugck + p2.get(i).text() +"\r\n";
			}
			bc.setBugck(bugck);

			Elements buding = el_bug.get(4).getElementsByClass("fl");
			if (buding.size() > 0) {
				Elements p4 = buding.get(0).getElementsByTag("a");
				String bugbd = "";
				for (int i = 0; i < p4.size(); i++) {
					bugbd = bugbd + p4.get(i).text() + "\r\n" + url + p4.get(i).attr("href");
				}
				bc.setBugbd(bugbd);
			}
		}
		return bugContent;
	}

	/**
	 * 检查关键字
	 * 
	 * @param title
	 * @return
	 */
	private boolean checkKeyWord(String title) {
		title = title.toLowerCase();
		boolean resule = false;
		String[] keys = keywords.split(",");
		for (int i = 0; i < keys.length; i++) {
			String key = keys[i];
			if (key.indexOf("-") > 0) {
				boolean allKey_flag = false;
				String[] allKeys = keywords.split("-");
				int trueCont = 0;
				for (int j = 0; j < allKeys.length; j++) {
					if (title.indexOf(allKeys[j]) >= 0) {
						trueCont++;
					}
				}
				if (trueCont == allKeys.length) {
					allKey_flag = true;
				}
				resule = allKey_flag;
				break;
			} else {
				if (title.indexOf(key) >= 0) {
					resule = true;
					break;
				}
			}
		}
		return resule;
	}
}
